package org.com.yz.common.util;

import org.com.yz.dal.dao.mapper.InterfaceConfigMapper;
import org.com.yz.dal.dao.mapper.MchConfigMapper;
import org.com.yz.dal.dao.model.InterfaceConfig;
import org.com.yz.dal.dao.model.MchConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CacheUtil implements  CacheUtils{

    @Autowired
    private InterfaceConfigMapper interfaceConfigMapper;

    @Autowired
    private MchConfigMapper mchConfigMapper;

    public static CacheUtil me() {
        return SpringContextHolder.getBean("cacheUtil");
    }

    //查询token
    @Override
    @Cacheable(value="token", key="'id'+#mchNo")
    public   String getMd5Token(String mchNo){
        if (ToolUtil.isEmpty(mchNo)) {
            return "--";
        }
        InterfaceConfig config = new InterfaceConfig();
        config.setMchNo(mchNo);
        List<InterfaceConfig> confs= interfaceConfigMapper.select(config);
        if(ToolUtil.isNotEmpty(confs)){
            config = confs.get(0);
        }
        if (ToolUtil.isNotEmpty(config) && ToolUtil.isNotEmpty(config.getMd5Token())) {
            return config.getMd5Token();
        }
        return "";
    }

    //查询token
    @Override
    @Cacheable(value="conf", key="'config'+#mchNo+'&'+#accntType")
    public MchConfig getMchConfigInfo(String mchNo,String accntType){
        MchConfig mchConfig = new MchConfig();
        if (ToolUtil.isEmpty(mchNo)) {
            return mchConfig;
        }
        mchConfig.setMchNo(mchNo);
        mchConfig.setAccntType(accntType);
        List<MchConfig> confs= mchConfigMapper.select(mchConfig);
        if(ToolUtil.isNotEmpty(confs)){
            mchConfig = confs.get(0);
        }
        return mchConfig;
    }



}
